/**
 * 
 */
package org.corpus.english.controller;

import java.util.Iterator;
import java.util.List;

import org.corpus.english.model.SentenceCategoryData;
import org.corpus.english.model.SentenceCategoryDataHome;
import org.corpus.english.view.SentenceCategoryTreeNode;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;


/**
 * @author robbiecheng
 *
 */
public class SentenceCategoryAdminController extends GenericForwardComposer{
	private SentenceCategoryDataHome scdDao = new SentenceCategoryDataHome();
	private SentenceCategoryData currentSCD = new SentenceCategoryData();
	private String newCategory = null;
	
	public String getNewCategory() {
		return newCategory;
	}

	public void setNewCategory(String newCategory) {
		this.newCategory = newCategory;
	}

	public SentenceCategoryData getCurrentSCD(){
		return currentSCD;
	}
	
	//save into current category
	public void onSelect$category(SelectEvent evt){
		currentSCD = (SentenceCategoryData)((SentenceCategoryTreeNode)((Treeitem)evt.getReference()).getValue()).getValue();
	}
	//insert a new sub-category
	private Tree category;
	public void onClick$save(){		
		if (currentSCD.getCategoryid() == 0)
			return;
		
		//save into database
		SentenceCategoryData newSCD = new SentenceCategoryData();
		newSCD.setParentid(currentSCD.getCategoryid());
		newSCD.setName(newCategory.trim());
		String fullname;
		if (currentSCD.getCategoryid() == 1)
			fullname = newCategory;
		else
			fullname = currentSCD.getFullname()+ "-" + newCategory;
		newSCD.setFullname(fullname);
		scdDao.persist(newSCD);	
		((SentenceCategoryTreeNode)category.getSelectedItem().getValue()).readChildren();
		
		newCurrentSCD();
	}
	

	//update category
	public void onClick$update(){
		if (currentSCD.getCategoryid() <= 1 )
			return;
		SentenceCategoryData parent = scdDao.findById(currentSCD.getParentid());
		String oldFullname = currentSCD.getFullname();
		String newFullname = currentSCD.getParentid() == 1 ? currentSCD.getName().trim() 
				: parent.getFullname() + "-" + currentSCD.getName().trim();
		
		//update currentSCD
		currentSCD.setFullname(newFullname);		
		scdDao.merge(currentSCD);
		
		//update all child SCD
		List children = scdDao.findByFullName(oldFullname + "-");
		for (Iterator iterator = children.iterator(); iterator.hasNext();) {
			SentenceCategoryData cate = (SentenceCategoryData) iterator.next();
			String newFn = newFullname + "-" + cate.getName();
			if (cate.getFullname().endsWith(newFn))
				continue;
			cate.setFullname(newFn);
			scdDao.merge(cate);
		}
		reloadSelecetedSCD();
	}
	
	//delete category
	public void onClick$delete(){
		if (currentSCD.getCategoryid() == 0)
			return;
		
		scdDao.delete(currentSCD);
		reloadSelecetedSCD();
	}
	
	private void reloadSelecetedSCD() {
		((SentenceCategoryTreeNode)category.getSelectedItem().getParentItem().getValue()).readChildren();
		newCurrentSCD();
	}
	
	private void newCurrentSCD() {
		currentSCD = new SentenceCategoryData();
		
	}

}
